package com.cjg.xiancheng;

public class syn {
    public static void main(String[] args) {
        buy buy = new buy();
        new Thread(buy,"11111").start();  new Thread(buy,"2222222222").start();  new Thread(buy,"33333333333").start();
    }
}
class  buy implements  Runnable{

    private boolean flag=true;
    private int tiket = 10;
    @Override
    public void run() {
        while (flag){
            try {
                buyTikets();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    private synchronized void buyTikets() throws InterruptedException {
        if (tiket<=0){
            flag=false;
            return;
        }
        System.out.println(Thread.currentThread().getName()+"获得第"+tiket--+"票");
        Thread.sleep(100);
    }
}
